<template>
    <!-- 注册时间 当前完成案件总数 姓名 导师 来源 申请时间 导师是否通过 -->
    <div>
        <div class='clearfloat mb10'>
            <orderStatistics class='fl' :myInfo ='myInfo' :roleID='$store.state.user.role_id'  @getData='getTableData'/>
            <table-search
                ref="table-search fr"
                :selectHead="selectHead"
                @searchMethods="searchMethods">
            </table-search>
        </div>
        <el-table
            @sort-change='tableSortChange'
            :data="tableData"
            border
        >
            <el-table-column
                label="ID"
                width="60"
                align="center">
                <template slot-scope="scope">
                    {{(scope.$index + 1) + (current_page - 1) * per_page}}
                </template>
            </el-table-column>
            <el-table-column
                sortable="custom"
                label="姓名"
                show-overflow-tooltip
                min-width="100"
                prop="name">
            </el-table-column>
            <el-table-column
                sortable="custom"
                label="联系方式"
                prop="phone"
                min-width="130"
                show-overflow-tooltip
                align="center">
            </el-table-column>
            <el-table-column
                sortable="custom"
                label="完成案件数(件)"
                align="center"
                prop="agent_finals_count"
                min-width="150">
            </el-table-column>
            <el-table-column
                sortable="custom"
                label="注册时间"
                align="center"
                prop="created_at"
                show-overflow-tooltip
                min-width="160">
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                min-width="180">
                <template slot-scope="scope">
                    <el-button type="primary" size="small" :disabled="scope.row.is_operated" @click="applyOperate(scope.row)">推荐出师</el-button>
                    <el-button type="warning" size="small" :disabled="scope.row.apply != 'true'" @click="auditStart(scope.row)">审核出师</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination @paginationChange="pageChange" :total="total"></pagination>
    </div>
</template>

<script>
import pagination from '~/components/pagination'
import { getMyInfo } from "~/api/user"
import tableSearch from '~/components/tableSearch'
import orderStatistics from "~/components/orderStatistics"
export default {
    components: {
        pagination,
        tableSearch,
        orderStatistics
    },

    data() {
        return {
            tableData: [],
            current_page: 1,
            per_page: 25,
            total: 0,
            selectHead: [
                {
                    label: '姓名',
                    props: 'name'
                },
                {
                    label: '联系方式',
                    props: 'phone'
                },
                {
                    label: '完成案件数',
                    props: 'agent_finals_count'
                },
                {
                    label: '注册时间',
                    props: 'created_at'
                }
            ],
            search_params: {},
            sort_params: {},
            myInfo: {
                graduationCount: 0,
            }
        }
    },

    created() {
        this.getTableData()
        // this.getMyinfo()
    },

    methods: {
        async getMyinfo () {
            let myInfo = await getMyInfo(this.$axios);
            if (myInfo.data.status == 'success') {
                this.$store.commit("setRoleInfo",myInfo.data.data)
                this.$store.commit("setUser",myInfo.data.data);
                this.myInfo.graduationCount = myInfo.data.data.graduationCount
            }
        },
        tableSortChange(column) {
            this.sort_params = {
                sort_str: column.prop
            }
            if (column.order == 'ascending') {
                this.sort_params.sort_order = 'asc'
            } else if (column.order == 'descending') {
                this.sort_params.sort_order = 'desc'
            } else {
                this.sort_params = {}
            }
            this.getTableData()
        },
        searchMethods(params, no_search) {
            this.search_params = params
            if (!no_search) {
                this.getTableData()
            }
        },
        applyOperate(data) {
            let params = {
                agent_id: data.id
            }
            this.$confirm(`确认推荐${data.name}出师吗？`, '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                center: true,
                type: 'warning'
            }).then(() => {
                this.$axios.post('/api/agent/recommend_graduation', params).then(res => {
                    if (res.data.code == 200) {
                        this.getMyinfo()
                        this.current_page = 1
                        this.getTableData()
                        this.$message({
                            type: 'success',
                            message: '推荐出师成功'
                        })
                    } else {
                        this.$message({
                            type: 'error',
                            message: res.data.message
                        })
                    }
                })
            }).catch(() => {})
        },
        auditStart (data) {
            let msg = `确认通过<span class="finish">${data.name}</span>的出师申请吗？`
            let params = {
                agent_id: data.id,
                pass: 'yes'
            }
            this.$confirm(msg, '提示', {
                type: 'warning',
                confirmButtonText: '通过',
                cancelButtonText: '拒绝',
                center: true,
                distinguishCancelAndClose: true,
                dangerouslyUseHTMLString: true
            }).then(() => {
                this.auditStartPost(params)

            }).catch(action => {
                if (action === 'cancel') {
                    params.pass = 'no'
                    this.auditStartPost(params)
                }
            });
        },
        auditStartPost(params) {
            this.$axios.post('/api/agent/teacher_graduation_audit', params).then(res => {
                if (res.data.code == 200) {
                    this.$message({
                        type: 'success',
                        message: `${params.pass == 'yes' ? '通过' : '拒绝'}出师申请成功`
                    })
                    this.current_page = 1
                    this.getMyinfo()
                    this.getTableData()
                } else {
                    this.$message({
                        type: 'error',
                        message:　res.data.message
                    })
                }
            })
        },
        pageChange(data) {
            this.per_page = data.perPage
            this.current_page = data.page
            this.getTableData()
        },
        getTableData(args) {
            let params = {
                page: this.current_page,
                perPage: this.per_page
            }
            params = {...params, ...this.sort_params, ...this.search_params}
            if (args) {
                params = {...params, ...args}
            }
            this.$axios.get('/api/agent/student_list', {
                params: params
            }).then(res => {
                if (res.data.code == 200) {
                    this.tableData = res.data.data
                    if (this.tableData.length > 0) {
                        this.tableData.forEach(v => {
                            v.is_operated = false
                            if (v.user_record[0].type == 3 || v.user_record[0].type == 2) {
                                v.is_operated = true
                            }
                            // if (v.user_record.findIndex(v => v.type == 6) > -1 || v.user_record.findIndex(v => v.type == 4) > -1) {
                            //     v.is_operated = false
                            // } else {
                            //     if (v.user_record.findIndex(v => v.type == 2) > -1 || v.user_record.findIndex(v => v.type == 3) > -1) {
                            //         v.is_operated = true
                            //     }
                            // }
                        });
                    }
                    this.total = res.data.page.total
                    this.getMyinfo()
                } else {
                    this.$message({
                        type: 'error',
                        message: res.data.message
                    })
                }
            })
        }
    },

    watch: {

    }
}
</script>

<style lang="less" scoped>

</style>
